/********************************************************************************/
/*																				*/
/*	Project:	Tianwan NPP FHM CCS												*/
/*	Library:	Twn_SpSetter.dll												*/
/*	Functions:	Contains the functions to calculate the Set points limits for 	*/
/*				FHM movements, in all plant zones								*/
/*	Author:		Matteo Tavella													*/
/*	Date:		04/02/2003														*/
/*																				*/
/********************************************************************************/
#define ALONE_TNW_SPS
#include "Twn_SpSetter.h"

int				cycleTime;
int				lastCycleNum;
float			quotes[NUM_SPS_QUOTES];
physPoint		perim[NUM_POINT];
circZone		react;
circZone		cont;
float			C12Axis;
float			C23Axis;
bool			always;

BOOL WINAPI DllMain(HINSTANCE hinstDLL,  // handle to DLL module
					DWORD fdwReason,     // reason for calling function
					LPVOID lpReserved )  // reserved
					{
	LONG	res;
	LPDWORD	type;
	LPBYTE	data;
	LPDWORD size;
	HKEY	hKey;
    DWORD	dwBufLen;
	DWORD	active;
    
	size = 0;
	data = 0;
	type = 0;
	
	
	switch(fdwReason){ 
		case DLL_PROCESS_ATTACH:
			RegOpenKeyEx(	HKEY_LOCAL_MACHINE, 
							TEXT("SOFTWARE\\ABB\\Power Generation Portal\\Tianwan\\Twn_SpSetter"),
							0,
							KEY_QUERY_VALUE,
							&hKey);
			active = 0;
			dwBufLen = sizeof(active);
			res = RegQueryValueEx(	hKey,
									TEXT("CycleTime"),
									NULL,
									NULL,
									(LPBYTE)&active,
									&dwBufLen);
			if(res == ERROR_SUCCESS){
				cycleTime = (int)active;
				if(cycleTime < 2000)
					cycleTime = 2000;
			}
			else{
				MessageBox(NULL, _T("Cannot load Sp Setter cycle time.\nSample time set default value (2 sec.)."), _T("Error"), MB_OK | MB_ICONERROR);
				cycleTime = 2000;
			}
			active = 0;
			dwBufLen = sizeof(active);
			res = RegQueryValueEx(	hKey,
									TEXT("LastCycleNum"),
									NULL,
									NULL,
									(LPBYTE)&active,
									&dwBufLen);
			if(res == ERROR_SUCCESS){
				lastCycleNum = (int)active;
				if(lastCycleNum < 2)
					lastCycleNum = 2;
			}
			else{
				MessageBox(NULL, _T("Cannot load Sp Setter last cycles number.\nParameter set to default value (2 cycles)."), _T("Warning"), MB_OK | MB_ICONWARNING);
				lastCycleNum = 2;
			}
			active = 0;
			dwBufLen = sizeof(active);
			res = RegQueryValueEx(	hKey,
									TEXT("RunAlways"),
									NULL,
									NULL,
									(LPBYTE)&active,
									&dwBufLen);
			if(res == ERROR_SUCCESS){
				if(active != 0)
					always = true;
				else 
					always = false;
			}
			else{
				MessageBox(NULL, _T("Cannot load RunAlways flag from registry.\nRunAlways deactivated."), _T("Warning"), MB_OK | MB_ICONWARNING);
				always = false;
			}
			RegCloseKey(hKey);
			break;
		default:
			break;
	}
    return TRUE;  // Successful DLL_PROCESS_ATTACH.
}

RETSTAT SpSetter(HWND hWnd){
	RETSTAT			retSt;
	RETCOND			retCnd;
	HWND			manHwnd;
	TAGIDX			tiStart;
	TAGIDX			tiBridgeMreq;
	TAGIDX			tiTrolleyMreq;
	TAGIDX			tiBridgeSp;
	TAGIDX			tiTrolleySp;
	TAGIDX			tiBridgePos;
	TAGIDX			tiTrolleyPos;
	TAGIDX			tiTVMount;
	TAGIDX			tiTVPos;
	TAGIDX			tiTVRMreq;
	TAGIDX			tiTVRSp;
	TAGIDX			tiMMPos;
	TAGIDX			tiMMSp;
	TAGIDX			tiMMMreq;
	TAGIDX			tiTVSp;
	TAGIDX			tiTVMreq;
	TAGIDX			tiMMClusMreq;
	TAGIDX			tiMMClusSp;
	TAGIDX			tiMMClusPos;
	TAGIDX			tiDoorC23;
	TAGIDX			tiDoorC12;
	TAGIDX			tiFALVLMount;
	TAGIDX			tiFALVLUpper;
	TAGIDX			tiFALVLLower;
	TAGIDX			tiDIMPMount;
	TAGIDX			tiMMDisplUp;
	TAGIDX			tiMMDisplDown;
	TAGIDX			tiMMFixOp;
	TAGIDX			tiMMFixCl;
	TAGIDX			tiMMTransp;
	TAGIDX			tiTVTransp;
	TAGIDX			tiMMRotPos;
	TAGIDX			tiMMRotSp;
	TAGIDX			tiMMTouchDown;
	TAGIDX			tiTVMPos;
	TAGIDX			tiMMFixReq;
	TAGIDX			tiMMUnfixReq;
	TAGIDX			tiMMClTransp;
	TAGIDX			tiMMRotFree;
	GENIDX			zoneIdx;
	GENIDX			contIdx;
	GENIDX			cellType;
	ZONENAME		zoneName;
	ZONENAME		supZoneName;
	TAGVALEX		tagVal;
	OBJTMM			mastSit;
	OBJTCELL		cellSit;
	TV_angle		rotRange;
	double			tCos;
	double			pi = 3.1415926535;
	float			px;
	float			py;
	float			bridgeLo;
	float			bridgeHi;
	float			trolleyLo;
	float			trolleyHi;
	float			temp1;
	float			temp2;
	float			temp3;
	float			temp4;
	float			tvAngle;
	float			x_off;
	float			y_off;
	float			tvUp;
	float			tvDown;
	float			tvLeft;
	float			tvRight;
	float			dummyLo;
	float			dummyHi;
	float			mmPos;
	float			mmClPos;
	float			mmTVDistance;
	float			falv_x;
	float			falv_y;
	float			rotToll;
	float			mmRot;
	float			TVMPos;
	int				opMode;
	int				vx;
	int				vy;
	int				nvx;
	int				nvy;
	int				rotFlag;
	int				cellIsEmpty;
	int				mastIsEmpty;
	int				num2pi;
	int				cellContIdx;
	int				refresh;
	int				lastCycle;
	int				reloadFlag;
	bool			movStarted;
	bool			dummy;
	bool			active;
	bool			first;
	bool			sendMsg;
	bool			tvMounted;
	bool			forceTV;
	bool			inC12;
	bool			inC23;
	bool			doorC12;
	bool			doorC23;
	bool			forceDoor;
	bool			mastBlocked;
	bool			inReact;
	bool			falvlMount;
	bool			falvlUpper;
	bool			dimpMount;
	bool			mmFixOp;
	bool			mmFixCl;
	bool			mmDisplUp;
	bool			mmDisplDown;
	bool			mmTransp;
	bool			tvTransp;
	bool			clTransp;
	bool			mmRotCorrect;
	bool			touchDown;
	bool			mastOnCell;
	bool			forceFALV;
	bool			forceDIMP;
	bool			mmRotFree;
	bool			tvOutRange;
	bool			mmRotFreeChange;

	lastCycle = 0;
	manHwnd = 0;
	if(hWnd != NULL)
		sendMsg = true;
	else
		sendMsg = false;

	if(cycleTime == 0)
		return(RT_NORMAL);

	retSt = LoadParam();
	if(retSt != RT_NORMAL){
		return(retSt);
	}

	retSt = RT_FAILED;

	retCnd = GetTagIdx(TWN_START_MOV, &tiStart);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_START_MOV"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_BRIDGE_MREQ, &tiBridgeMreq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_BRIDGE_MREQ"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TROLLEY_MREQ, &tiTrolleyMreq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TROLLEY_MREQ"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TV_ROT_MREQ, &tiTVRMreq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TV_ROT_MREQ"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_BRIDGE_SP, &tiBridgeSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_BRIDGE_SP"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TROLLEY_SP, &tiTrolleySp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TROLLEY_SP"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_BRIDGE_POS, &tiBridgePos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_BRIDGE_POS"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TROLLEY_POS, &tiTrolleyPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TROLLEY_POS"), _T("Error"), MB_OK);
		return(retSt);
	}
	
	retCnd = GetTagIdx(TWN_TV_ROT_POS, &tiTVPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TV_ROT_POS"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TV_MOUNT, &tiTVMount);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TV_MOUNT"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TV_ROT_SP, &tiTVRSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TV_ROT_SP"), _T("Error"), MB_OK);
		return(retSt);
	}
	
	retCnd = GetTagIdx(TWN_FAGR_SP, &tiMMSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FAGR_SP"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_FAGR_MREQ, &tiMMMreq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FAGR_MREQ"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TVM_SP, &tiTVSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TVM_SP"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_TVM_MREQ, &tiTVMreq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TVM_MREQ"), _T("Error"), MB_OK);
		return(retSt);
	}

	retCnd = GetTagIdx(TWN_CLGR_MREQ, &tiMMClusMreq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_CLGR_MREQ"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_CLGR_SP, &tiMMClusSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_CLGR_SP"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_DOORZ2Z3_CL, &tiDoorC23);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_DOORZ2Z3_CL"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_DOORZ1Z2_CL, &tiDoorC12);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_DOORZ1Z2_CL"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_FAGR_POS, &tiMMPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FAGR_POS"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_CLGR_POS, &tiMMClusPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_CLGR_POS"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	
	retCnd = GetTagIdx(TWN_FALVL_MOUNT, &tiFALVLMount);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FALVL_MOUNT"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_FALVL_UPPER, &tiFALVLUpper);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FALVL_UPPER"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_FALVL_LOWER, &tiFALVLLower);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FALVL_LOWER"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_DIMP_MOUNT, &tiDIMPMount);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_DIMP_MOUNT"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_MM_DISPL_UP, &tiMMDisplUp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_MM_DISPL_UP"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_MM_DISPL_DOWN, &tiMMDisplDown);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_MM_DISPL_DOWN"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_FAGRL_UNFIXED, &tiMMFixOp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FAGRL_UNFIXED"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_FAGRL_FIXED, &tiMMFixCl);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_FAGRL_FIXED"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_MM_TRANSPOS, &tiMMTransp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_MM_TRANSPOS"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_TV_TRANS_POS, &tiTVTransp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_TV_TRANS_POS"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_MM_ROT_POS, &tiMMRotPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_MM_ROT_POS"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_MM_ROT_SP, &tiMMRotSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_MM_ROT_SP"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_MM_TOUCHDOWN, &tiMMTouchDown);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag idx TWN_MM_TOUCHDOWN"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_TVM_POS, &tiTVMPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TVM_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_FAGRL_FIX_REQ, &tiMMFixReq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_FAGRL_FIX_REQ idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_FAGRL_UNFIX_REQ, &tiMMUnfixReq);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_FAGRL_UNFIX_REQ idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_MM_CL_TRANSP, &tiMMClTransp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_MM_CL_TRANSP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagIdx(TWN_MM_ROT_FREE, &tiMMRotFree);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_MM_ROT_FREE idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retSt = CheckMov(tiStart, &movStarted);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error checking movement in execution."), _T("Error"), MB_OK);
		return(retSt);
	}

	retSt = SetDefaultLimits();
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error setting default sp limits."), _T("Error"), MB_OK);
		return(retSt);
	}

	retSt = GetMMTVDistance(&mmTVDistance);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading MM-TV mast ditance from Infi90 block"), _T("Error"), MB_OK);
		return(retSt);
	}

	GetFALVOffset(&falv_x, &falv_y);

	GetMMRotToll(&rotToll);

	retSt = GetDynCurrentMode(&opMode);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error getting current operating mode."), _T("Error"), MB_OK);
		goto SLEEP;
	}
	
	if(opMode != M_PROTECT)
		active = false;
	else
		active = false;

	retCnd = TagRead(&tiTVMount, &tagVal);
	if(retCnd != R_NORMAL)
		tvMounted = false;
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			tvMounted = true;
		else
			tvMounted = false;
	}

	retCnd = TagRead(&tiDoorC12, &tagVal);
	if(retCnd != R_NORMAL)
		doorC12 = true;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		doorC12 = true;
	else
		doorC12 = false;

	retCnd = TagRead(&tiDoorC23, &tagVal);
	if(retCnd != R_NORMAL)
		doorC23 = true;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		doorC23 = true;
	else
		doorC23 = false;

	retCnd = TagRead(&tiFALVLMount, &tagVal);
	if(retCnd != R_NORMAL)
		falvlMount = false;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		falvlMount = true;
	else
		falvlMount = false;

	retCnd = TagRead(&tiDIMPMount, &tagVal);
	if(retCnd != R_NORMAL)
		dimpMount = false;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		dimpMount = true;
	else
		dimpMount = false;

	retCnd = TagRead(&tiMMRotFree, &tagVal);
	if(retCnd != R_NORMAL)
		mmRotFree = false;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		mmRotFree = true;
	else
		mmRotFree = false;

	first = true;
	lastCycle = 0;

	while(1){
		retSt = GetDynCurrentMode(&opMode);
		if(retSt != RT_NORMAL)
			goto SLEEP;

		if(opMode != M_PROTECT){
			if(active){
				active = false;
				retSt = SetDefaultLimits();
				if(retSt != RT_NORMAL)
					goto SND_MSG;
			}
			goto SLEEP;
		}
		else{
			if(!active)
				first = true;
			else
				first = false;
			active = true;
		}
		forceTV= false;
		retCnd = TagRead(&tiTVMount, &tagVal);
		if(retCnd != R_NORMAL)
			tvMounted = false;
		else{
			if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !tvMounted){
				tvMounted = true;
				forceTV= true;
			}
			else if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && tvMounted){
				tvMounted = false;
				forceTV= true;
			}
		}

		forceDoor = false;
		retCnd = TagRead(&tiDoorC12, &tagVal);
		if(retCnd != R_NORMAL)
			goto SLEEP;
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			if(!doorC12)
				forceDoor = true;
			doorC12 = true;
		}
		else{
			if(doorC12)
				forceDoor = true;
			doorC12 = false;
		}

		if(!forceDoor){
			retCnd = TagRead(&tiDoorC23, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
				if(!doorC23)
					forceDoor = true;
				doorC23 = true;
			}
			else{
				if(doorC23)
					forceDoor = true;
				doorC23 = false;
			}
		}

		forceFALV = false;
		retCnd = TagRead(&tiFALVLMount, &tagVal);
		if(retCnd != R_NORMAL)
			forceFALV = false;
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			if(falvlMount)
				forceFALV = false;
			else
				forceFALV = true;
			falvlMount = true;
		}
		else{
			if(falvlMount)
				forceFALV = true;
			else
				forceFALV = false;
			falvlMount = false;
		}

		forceDIMP = false;
		retCnd = TagRead(&tiDIMPMount, &tagVal);
		if(retCnd != R_NORMAL)
			forceDIMP = false;
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			if(dimpMount)
				forceDIMP = false;
			else
				forceDIMP = true;
			dimpMount = true;
		}
		else{
			if(dimpMount)
				forceDIMP = true;
			else
				forceDIMP = false;
			dimpMount = false;
		}

		retCnd = TagRead(&tiMMRotFree, &tagVal);
		if(retCnd != R_NORMAL){
			mmRotFreeChange = false;
			mmRotFree = false;
		}
		if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !mmRotFree)
			mmRotFreeChange = true;
		if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && mmRotFree)
			mmRotFreeChange = true;
		else
			mmRotFreeChange = false;

		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			mmRotFree = true;
		else
			mmRotFree = false;

		retSt = GetDynSPReloadPar(&reloadFlag);

		if(reloadFlag != 0){
			LoadParam();
			SetDynSPReloadPar(0);
		}

		GetDynSPRefresh(&refresh);

		movStarted = dummy;

		retSt = CheckMov(tiStart, &dummy);
		if(retSt != RT_NORMAL)
			goto SLEEP;
		
		if(!dummy && movStarted)
			lastCycle = lastCycleNum;

		if(dummy || (lastCycle > 0) || first || forceTV || forceDoor || always || (refresh != 0) || (reloadFlag != 0) || forceFALV || forceDIMP || mmRotFreeChange){
			first = false;

			if(lastCycle > 0)
				lastCycle--;

			if(refresh != 0)
				SetDynSPRefresh(0);
			
			if(reloadFlag != 0)
				reloadFlag = 0;

			//X,Y//
			retCnd = TagRead(&tiBridgePos, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
		
			px = tagVal.value.val_float;

			retCnd = TagRead(&tiTrolleyPos, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
		
			py = tagVal.value.val_float;

			//FALV//
			retCnd = TagRead(&tiFALVLMount, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
			
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				falvlMount = true;
			else
				falvlMount = false;

			//DIMP//
			retCnd = TagRead(&tiDIMPMount, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
			
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				dimpMount = true;
			else
				dimpMount = false;

			if(falvlMount){
				retCnd = TagRead(&tiFALVLUpper, &tagVal);
				if(retCnd != R_NORMAL)
					goto SLEEP;
					
				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					falvlUpper = true;
				else
					falvlUpper = false;

				px = (float)(px + falv_x);
				py = (float)(py + falv_y);
			}
			else
				falvlUpper = true;

			if(dimpMount){
				px = (float)(px + falv_x);
				py = (float)(py + falv_y);
			}

			//MAIN MAST//
			retCnd = TagRead(&tiMMPos, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
		
			mmPos = tagVal.value.val_float;

			retCnd = TagRead(&tiMMClusPos, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
		
			mmClPos = tagVal.value.val_float;

			retCnd = TagRead(&tiMMTransp, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
				mmTransp = true;
			else
				mmTransp = false;

			retCnd = TagRead(&tiMMClTransp, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
				clTransp = true;
			else
				clTransp = false;

			retCnd = TagRead(&tiMMFixOp, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
				mmFixOp = true;
			else
				mmFixOp = false;

			retCnd = TagRead(&tiMMFixCl, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
				mmFixCl = true;
			else
				mmFixCl = false;

			//TV MAST//
			if(tvMounted && !falvlMount && !dimpMount){
				retCnd = TagRead(&tiTVTransp, &tagVal);
				if(retCnd != R_NORMAL)
					goto SLEEP;

				if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
					tvTransp = true;
				else
					tvTransp = false;

				retCnd = TagRead(&tiTVMPos, &tagVal);
				if(retCnd != R_NORMAL)
					goto SLEEP;

				TVMPos = tagVal.value.val_float;
				
				retCnd = TagRead(&tiTVPos, &tagVal);
				if(retCnd != R_NORMAL)
					goto SLEEP;
			
				if((tagVal.value.val_float >= (float)(0.0 - TOLL_MM_GR)) &&
					(tagVal.value.val_float <= (float)(315.0 + TOLL_MM_GR))){
					if(tagVal.value.val_float < 0.0)
						tvAngle = 0.0;
					else
						tvAngle = tagVal.value.val_float;

				}

				else if(tagVal.value.val_float < (float)(0.0 - TOLL_MM_GR)){
					num2pi = abs((int)(tagVal.value.val_float / 360.0));
					tvAngle = (float)(360.0 - fabs(tagVal.value.val_float + (float)(num2pi * 360.0)));
				}
				else if(tagVal.value.val_float > (float)(360.0)){
					num2pi = abs((int)(tagVal.value.val_float / 360.0));
					tvAngle = (float)(fabs(tagVal.value.val_float - (float)(num2pi * 360.0)));
				}
			
				if((tvAngle >= (float)(0.0 + TOLL_MM_GR)) && (tvAngle <= (float)(180.0 - TOLL_MM_GR)))
					tvDown = 1.0;
				else
					tvDown = 0.0;
				
				if((tvAngle >= (float)(180.0 + TOLL_MM_GR)) && (tvAngle <= (float)(360.0 - TOLL_MM_GR)))
					tvUp = 1.0;
				else
					tvUp = 0.0;
				
				if((tvAngle >= (float)(90.0 + TOLL_MM_GR)) && (tvAngle <= (float)(270.0 - TOLL_MM_GR)))
					tvRight = 1.0;
				else
					tvRight = 0.0;

				if((tvAngle >= (float)(270.0 + TOLL_MM_GR)) || (tvAngle <= (float)(90.0 - TOLL_MM_GR)))
					tvLeft = 1.0;
				else
					tvLeft = 0.0;

				tCos = cos((double)(((360.0 - tvAngle) * (2 * pi)) / 360.0));
				x_off = (float)(mmTVDistance * (float)(tCos));
				tCos = sin((double)(((360.0 - tvAngle) * (2 * pi)) / 360.0));
				y_off = (float)((-1.0) * mmTVDistance * (float)(tCos));
			}
			else{
				tvDown = 0.0;
				tvUp = 0.0;
				tvRight = 0.0;
				tvLeft = 0.0;
				x_off = 0.0;
				y_off = 0.0;
				tvAngle = 0.0;
				TVMPos = 0.0;
				tvTransp = true;
			}

			//DISPL//
			retCnd = TagRead(&tiMMDisplDown, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
				mmDisplDown = true;
			else
				mmDisplDown = false;
			
			retCnd = TagRead(&tiMMDisplUp, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;

			if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
				mmDisplUp = true;
			else
				mmDisplUp = false;

			//MM ROT//
			retCnd = TagRead(&tiMMRotPos, &tagVal);
			if(retCnd != R_NORMAL){
				mmRot = 0.0;
				mmRotCorrect = true;
			}
			else{
				mmRot = (float)(tagVal.value.val_float);
				
				if(((mmRot >= (float)(0.0 - rotToll)) && (mmRot <= (float)(0.0 + rotToll)))		||
				   ((mmRot >= (float)(45.0 - rotToll)) && (mmRot <= (float)(45.0 + rotToll)))	||
				   ((mmRot >= (float)(180.0 - rotToll)) && (mmRot <= (float)(180.0 + rotToll))))
					mmRotCorrect = true;
				else
					mmRotCorrect = false;
			}

			retCnd = TagRead(&tiMMRotFree, &tagVal);
			if(retCnd != R_NORMAL)
				mmRotFree = false;
			else{
				if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
					mmRotFree = true;
				else
					mmRotFree = false;
			}

			//CELL AND MAST
			mastOnCell = false;
			retSt = GetMastSituation(&mastSit);
			if(retSt != RT_NORMAL)
				goto SLEEP;

			mastIsEmpty = 0;
			retSt = MastIsEmpty(&mastIsEmpty);
			if(retSt != RT_NORMAL)
				goto SLEEP;

			retSt = GetZonePhisVirtCoord(px, py, &vx, &vy);
			if(retSt == RT_FAILED)
				mastOnCell = false;
			else{
				mastOnCell = true;
				retSt = GetCellSituation(vx, vy, &cellSit);
				if(retSt != RT_NORMAL)
					goto SLEEP;

				GetCellType(vx, vy, &cellType);

				cellIsEmpty = 0;

				retSt = CellIsEmpty(vx, vy, &cellIsEmpty);
				if(retSt != RT_NORMAL)
					goto SLEEP;
			}

			retSt = GetZoneIdxPhisCooSC(px, py, &zoneIdx);
			if(retSt == RT_NORMAL){
				retSt = GetZoneName(zoneIdx, zoneName);
				if(retSt != RT_NORMAL)
					goto SLEEP;
				retSt = GetZoneSuperZone(zoneName, supZoneName);
				if(retSt != RT_NORMAL)
					goto SLEEP;
			}else{
				if(IsCorrPosition(px, py) == RT_FAILED)
					goto SLEEP;
			}
	
			retCnd = TagRead(&tiMMTouchDown, &tagVal);
			if(retCnd != R_NORMAL)
				goto SLEEP;
			if(GtBit((unsigned int)(tagVal.value.val_di), 15) == 0)
				touchDown = true;
			else
				touchDown = false;
			
			if(falvlMount || dimpMount){
				InsideCircle(react.center, react.radius, px, py, &inReact);
				if(!inReact){
					retCnd = Set_sp((float)(px - falv_x), (float)(px - falv_x), (float)(py - falv_y), (float)(py - falv_y), tiBridgeSp, tiTrolleySp);
					goto SP_TVR;
				}
			}

			inC12 = false;
			inC23 = false;
			inReact = false;
			contIdx = 0;
			if((px >= (cont.center.x - cont.radius)) && (px <= (cont.center.x + cont.radius)) &&
			   (py >= (cont.center.y - cont.radius)) && (py <= (cont.center.y + cont.radius))){
				//Z1//
				GetDynCurrentCont(&contIdx);
				if((py >= (float)(C12Axis - TOLL_MM)) && (py <= (float)(C12Axis + TOLL_MM))){
					//Y-->C12///
					GetCircX12(cont.center, cont.radius, py - y_off, &temp1, &temp2);
					if(!tvUp &&!tvDown){
						bridgeLo = temp1;
						if(doorC12)
							bridgeHi = perim[14].x;
						else
							bridgeHi = perim[3].x;
					}
					else{
						bridgeLo = temp1;
						bridgeHi = temp2;
					}
				}
				else{
					GetCircX12(cont.center, cont.radius, py - y_off, &temp1, &temp2);
					GetCircX12(cont.center, cont.radius, py, &temp3, &temp4);
					if((temp4 - temp3) < (temp2 - temp1)){
						bridgeLo = temp3;
						bridgeHi = temp4;
					}
					else{
						bridgeLo = temp1;
						bridgeHi = temp2;
					}
				}
				GetCircY12(cont.center, cont.radius, px - x_off, &temp1, &temp2);
				GetCircY12(cont.center, cont.radius, px, &temp3, &temp4);
				if((temp4 - temp3) < (temp2 - temp1)){
					trolleyLo = temp3;
					trolleyHi = temp4;
				}
				else{
					trolleyLo = temp1;
					trolleyHi = temp2;
				}	
				//SP Y extra manag for TV in corridors...//
				if((py >= (float)(C12Axis - TOLL_MM)) && (py <= (float)(C12Axis + TOLL_MM))){
					if(tvRight){
						if((px - x_off) >= (float)(perim[0].x  - TOLL_MM)){
							trolleyLo = (float)(C12Axis - TOLL_MM);
							trolleyHi = (float)(C12Axis + TOLL_MM);
						}
					}
				}
			}
			else if((px >= (react.center.x - react.radius)) && (px <= (react.center.x + react.radius)) &&
			        (py >= (react.center.y - react.radius)) && (py <= (react.center.y + react.radius))){
				//Z3//
				inReact = true;
				if(!tvMounted || falvlMount || dimpMount){
					if((py >= (float)(C23Axis - TOLL_MM)) && (py <= (float)(C23Axis + TOLL_MM))){
						//Y-->C23///
						GetCircX12(react.center, react.radius, py , &temp3, &temp4);
						if(doorC23)
							bridgeLo = perim[6].x;
						else
							bridgeLo = perim[9].x;
						bridgeHi = temp4;
					}
					else{
						GetCircX12(react.center, react.radius, py, &temp3, &temp4);
						bridgeLo = temp3;
						bridgeHi = temp4;	
					}
					GetCircY12(react.center, react.radius, px, &temp3, &temp4);
					trolleyLo = temp3;
					trolleyHi = temp4;
				}
				else{
					if((!doorC23) && (!tvUp) && (!tvDown) && (py >= (float)(C23Axis - TOLL_MM)) && (py <= (float)(C23Axis + TOLL_MM))){
						//Y-->C23///
						GetCircX12(react.center, react.radius, py , &temp3, &temp4);
						if(doorC23){
							if(tvLeft)
								bridgeLo = perim[6].x + x_off;
							else
								bridgeLo = perim[6].x;
						}
						else{
							if(tvLeft)
								bridgeLo = perim[9].x + x_off;
							else
								bridgeLo = perim[9].x;
						}
						if(tvRight)
							bridgeHi = temp4 + x_off;
						else
							bridgeHi = temp4;
					}
					else{
						GetCircX12(react.center, react.radius, py - y_off, &temp1, &temp2);
						GetCircX12(react.center, react.radius, py, &temp3, &temp4);
						if(((temp1 != -1.0) && (temp2 != -1.0)) || ((temp3 != -1.0) && (temp4 != -1.0))){
							if(tvLeft){
								bridgeLo = (float)(temp1 + TOLL_MM + x_off);
								bridgeHi = (float)(temp4 - TOLL_MM);
							}
							else if(tvRight){
								bridgeLo = (float)(temp3 + TOLL_MM);
								bridgeHi = (float)(temp2 - TOLL_MM + x_off);
							}
							else{
								if((temp2 - temp1) < (temp4 - temp3)){
									bridgeLo = temp1;
									bridgeHi = temp2;
								}
								else{
									bridgeLo = temp3;
									bridgeHi = temp4;
								}	
							}
						}
						else{
							bridgeLo = px;
							bridgeHi = px;
						}
					}
					if(((px - x_off) <= (float)(perim[5].x + TOLL_MM)) && (py >= (float)(perim[7].y + TOLL_MM)) && (py <= (float)(perim[4].y - TOLL_MM)) && tvLeft){
						trolleyLo = (float)(perim[7].y + TOLL_MM);
						trolleyHi = (float)(perim[4].y - TOLL_MM);
					}
					else{
						GetCircY12(react.center, react.radius, px - x_off, &temp1, &temp2);
						GetCircY12(react.center, react.radius, px, &temp3, &temp4);
						if((temp1 == -1.0) || (temp2 == -1.0) || (temp3 == -1.0) || (temp4 == -1.0)){
							trolleyLo = py;
							trolleyHi = py;
						}
						else{
							if(tvDown){
								trolleyLo = (float)(temp1 + TOLL_MM + y_off);
								trolleyHi = temp4;
							}
							else if(tvUp){
								trolleyLo = temp3;
								trolleyHi = (float)(temp2 - TOLL_MM + y_off);
							}
							else{
								if((temp2 - temp1) < (temp4 - temp3)){
									trolleyLo = temp1;
									trolleyHi = temp2;
								}
								else{
									trolleyLo = temp3;
									trolleyHi = temp4;
								}
							}
						}
					}
				}
			}
			else if((px >= perim[0].x) && (px <= perim[1].x) &&
			        (py >= perim[14].y) && (py <= perim[0].y)){
				//C1-2//
				inC12 = true;
				GetCircX12(cont.center, cont.radius, py - y_off, &temp1, &temp2);
				GetCircX12(cont.center, cont.radius, py, &temp3, &temp4);
				if(temp1 > temp3)
					bridgeLo = temp1;
				else
					bridgeLo = temp3;
				bridgeHi = perim[3].x;
				trolleyLo = (float)(C12Axis - TOLL_MM);
				trolleyHi = (float)(C12Axis + TOLL_MM);

			}
			else if((px >= perim[4].x) && (px <= perim[5].x) &&
			        (py >= perim[7].y) && (py <= perim[4].y)){
				//C2-3//
				inC23 = true;
				bridgeLo = perim[9].x;
				GetCircX12(react.center, react.radius, py - y_off, &temp1, &temp2);
				GetCircX12(react.center, react.radius, py, &temp3, &temp4);
				if(temp2 < temp4)
					bridgeHi = temp2;
				else
					bridgeHi = temp4;
				trolleyLo = (float)(C23Axis - TOLL_MM);
				trolleyHi = (float)(C23Axis + TOLL_MM);


			}
			else{
				//Z2//
				//SP X//	
				if((py >= perim[9].y) && (py < perim[10].y)){
					//Lower Z2//
					if((py >= (float)(C23Axis - TOLL_MM)) && (py <= (float)(C23Axis + TOLL_MM))){
						//Y-->C23///
						bridgeLo = (float)(perim[9].x);
						if(!tvUp && !tvDown){
							if(doorC23)
								bridgeHi = perim[7].x;
							else{
								GetCircX12(react.center, react.radius, py - y_off, &temp1, &temp2);
								bridgeHi = temp2;
							}
						}
						else
							bridgeHi = perim[8].x;
					}
					else{
						bridgeLo = perim[9].x;
						bridgeHi = perim[8].x;
					}
				}
				else if((py >= perim[10].y) && (py < perim[12].y)){
					//Mid Z2//
					if(tvUp)
						GetZ23X(perim[11], perim[12], py - y_off, &bridgeLo);
					else
						GetZ23X(perim[11], perim[12], py, &bridgeLo);
					bridgeHi = (perim[8].x);
				}
				else if((py >= perim[12].y) && (py <= perim[2].y)){
					//Higher Z2//
					if((py >= (float)(C12Axis - TOLL_MM)) && (py <= (float)(C12Axis + TOLL_MM))){
						//Y-->C12///
						bridgeHi = perim[3].x;
						if(!tvUp && !tvDown){
							if(doorC12)
								bridgeLo = perim[2].x;
							else{
								GetCircX12(cont.center, cont.radius, py - y_off, &temp1, &temp2);
								bridgeLo = temp1;
							}
						}
						else
							bridgeLo = perim[2].x;
					}
					else{
						bridgeLo = perim[2].x;
						bridgeHi = perim[3].x;
					}
				}
				
				//SP Y//
				if((px >= perim[10].x) && (px < perim[11].x)){
					//Left Z2//
					trolleyLo = perim[9].y;
					trolleyHi = perim[10].y;
				}
				else if((px >= perim[11].x) && (px < perim[12].x)){
					//Mid Z2//
					trolleyLo = perim[9].y;
					
					if(tvLeft)
						GetZ23Y(perim[11], perim[12], px - x_off, &trolleyHi);
					else
						GetZ23Y(perim[11], perim[12], px, &trolleyHi);
				}
				else if((px >= perim[12].x) && (px <= perim[3].x)){
					//Right Z2//
					trolleyLo = perim[9].y;
					trolleyHi = perim[2].y;
				}

				//SP Y extra manag for TV in corridors...//
				if((py >= (float)(C12Axis - TOLL_MM)) && (py <= (float)(C12Axis + TOLL_MM))){
					if(tvLeft){
						if((px - x_off) <= (float)(perim[2].x + TOLL_MM)){
							trolleyLo = (float)(C12Axis - TOLL_MM);
							trolleyHi = (float)(C12Axis + TOLL_MM);
						}
					}

				}
				else if((py >= (float)(C23Axis - TOLL_MM)) && (py <= (float)(C23Axis + TOLL_MM))){
					if(tvRight){
						if((px - x_off) >= (float)(perim[7].x  - TOLL_MM)){
							trolleyLo = (float)(C23Axis - TOLL_MM);
							trolleyHi = (float)(C23Axis + TOLL_MM);
						}
					}
				}
			}

			if(falvlMount){
				if(!inReact || !falvlUpper)
					retCnd = Set_sp((float)(px - falv_x), (float)(px - falv_x), (float)(py - falv_y), (float)(py - falv_y), tiBridgeSp, tiTrolleySp);
				else
					retCnd = Set_sp((float)(bridgeLo - falv_x), (float)(bridgeHi - falv_x), (float)(trolleyLo - falv_y), (float)(trolleyHi - falv_y), tiBridgeSp, tiTrolleySp);
			}
			else if(dimpMount){
				if(!inReact)
					retCnd = Set_sp((float)(px - falv_x), (float)(px - falv_x), (float)(py - falv_y), (float)(py - falv_y), tiBridgeSp, tiTrolleySp);
				else
					retCnd = Set_sp((float)(bridgeLo - falv_x), (float)(bridgeHi - falv_x), (float)(trolleyLo - falv_y), (float)(trolleyHi - falv_y), tiBridgeSp, tiTrolleySp);
			}
			else if(!mmTransp || !tvTransp || !clTransp){
				retCnd = Set_sp(px, px, py, py, tiBridgeSp, tiTrolleySp);
			}
			else{
				if(!inReact)
					retCnd = Set_sp((float)(bridgeLo + (tvLeft * x_off)), (float)(bridgeHi + (tvRight * x_off)), (float)(trolleyLo + (tvDown * y_off)), (float)(trolleyHi + (tvUp * y_off)), tiBridgeSp, tiTrolleySp);
				else
					retCnd = Set_sp(bridgeLo, bridgeHi, trolleyLo, trolleyHi, tiBridgeSp, tiTrolleySp);
			}
SP_TVR:
			//TV rotation limitations//
			if(tvMounted){
				//SP Y extra manag for TV in corridors...//
				if(CheckAngleInRange(rotRange, tvAngle) == RT_NORMAL)
					tvOutRange = false;
				else
					tvOutRange = true;

				if(!inC23){
					if((py >= (float)(C23Axis - TOLL_MM)) && (py <= (float)(C23Axis + TOLL_MM))){
						if((tvRight || tvLeft) && (((px - x_off) >= perim[4].x) && ((px - x_off) <= perim[5].x))){
							retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
							goto SP_TVM;
						}
					}
				}

				if(!inC12){
					if((py >= (float)(C12Axis - TOLL_MM)) && (py <= (float)(C12Axis + TOLL_MM))){
						if((tvRight || tvLeft) && (((px - x_off) >= perim[0].x) && ((px - x_off) <= perim[1].x))){
							retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
							goto SP_TVM;
						}

					}
				}

				if(!inC12 && !inC23){
					retSt = GetZoneNearCell(px, py, &nvx, &nvy, &rotFlag, &rotRange, false);
					if(retSt != RT_NORMAL)
						goto SP_TVM;
				}
				
				if((rotFlag == 0) || inC12 || inC23 || falvlMount || dimpMount){
					retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
					goto SP_TVM;
				}
				
				if(rotFlag == 1){
					tvOutRange = true;
					if((rotRange.range_1[0] == -1) &&
					   (rotRange.range_1[1] == -1) &&
					   (rotRange.range_2[0] == -1) &&
					   (rotRange.range_2[1] == -1)){
						retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
						if(retCnd != R_NORMAL)
							goto SP_TVM;
					}
					else if((rotRange.range_1[0] != -1) &&
							(rotRange.range_1[1] == -1) &&
							(rotRange.range_2[0] == -1) &&
							(rotRange.range_2[1] == -1)){
						if((tvAngle >= (float)(rotRange.range_1[0] - TOLL_MM)) && (tvAngle <= (float)(rotRange.range_1[0] + TOLL_MM)))
							tvOutRange = false;
				 		retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
						if(retCnd != R_NORMAL)
							goto SP_TVM;
					}
					else if((rotRange.range_1[0] != -1) &&
							(rotRange.range_1[1] == -1) &&
							(rotRange.range_2[0] != -1) &&
							(rotRange.range_2[1] == -1)){ 
						if(((tvAngle >= (float)(rotRange.range_1[0] - TOLL_MM)) && (tvAngle <= (float)(rotRange.range_1[0] + TOLL_MM)))	||
						   ((tvAngle >= (float)(rotRange.range_2[0] - TOLL_MM)) && (tvAngle <= (float)(rotRange.range_2[0] + TOLL_MM))))
							tvOutRange = false;
				 		retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
						if(retCnd != R_NORMAL)
							goto SP_TVM;
					}
					else if((rotRange.range_1[0] != -1) &&
							(rotRange.range_1[1] != -1) &&
							(rotRange.range_2[0] == -1) &&
							(rotRange.range_2[1] == -1)){
						if((tvAngle >= (float)(rotRange.range_1[0] - TOLL_MM_GR)) && (tvAngle <= (float)(rotRange.range_1[1] + TOLL_MM_GR))){
				 			tvOutRange = false;
							retCnd = SetTagSp(rotRange.range_1[0], rotRange.range_1[1], tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
						else{
							retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
					}
					else if((rotRange.range_1[0] != -1) &&
							(rotRange.range_1[1] != -1) &&
							(rotRange.range_2[0] != -1) &&
							(rotRange.range_2[1] == -1)){
						if((tvAngle >= (float)(rotRange.range_1[0] - TOLL_MM_GR)) && (tvAngle <= (float)(rotRange.range_1[1] + TOLL_MM_GR))){
				 			tvOutRange = false;
							retCnd = SetTagSp(rotRange.range_1[0], rotRange.range_1[1], tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
						else{
							if((tvAngle >= (float)(rotRange.range_2[0] - TOLL_MM)) && (tvAngle <= (float)(rotRange.range_2[0] + TOLL_MM)))
								tvOutRange = false;
							retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
					}
					else if((rotRange.range_1[0] != -1) &&
							(rotRange.range_1[1] != -1) &&
							(rotRange.range_2[0] != -1) &&
							(rotRange.range_2[1] != -1)){
						if((tvAngle >= (float)(rotRange.range_1[0] - TOLL_MM_GR)) && (tvAngle <= (float)(rotRange.range_1[1] + TOLL_MM_GR))){
				 			tvOutRange = false;
							retCnd = SetTagSp(rotRange.range_1[0], rotRange.range_1[1], tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
						else if((tvAngle >= (float)(rotRange.range_2[0] - TOLL_MM_GR)) && (tvAngle <= (float)(rotRange.range_2[1] + TOLL_MM_GR))){
				 			tvOutRange = false;
							retCnd = SetTagSp(rotRange.range_2[0], rotRange.range_2[1], tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
						else{
							retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
							if(retCnd != R_NORMAL)
								goto SP_TVM;
						}
					}
				}
			}
			else{
				if(CheckAngleInRange(rotRange, tvAngle) == RT_NORMAL)
					tvOutRange = false;
				else
					tvOutRange = true;
				retCnd = SetTagSp(tvAngle, tvAngle, tiTVRSp);
				if(retCnd != R_NORMAL)
					goto SP_TVM;	
			}
SP_TVM:		
			//TV mast limitations//
			if(tvMounted){
				dummyLo = quotes[QUO_TV_TP];
				if(inC12 || inC23){		
					retCnd = SetTagSp(dummyLo, quotes[QUO_TV_TP], tiTVSp);	
					goto SP_MM;
				}

				if(tvOutRange){
					retCnd = SetTagSp(dummyLo, dummyLo, tiTVSp);	
					goto SP_MM;
				}

				if(!_tcscmp(supZoneName, ZONE_IO)){
					dummyHi = quotes[QUO_TV_Z1];
				}
				else if(!_tcscmp(supZoneName, ZONE_FA)){
					dummyHi = quotes[QUO_TV_Z21];
				}	
				else if(!_tcscmp(supZoneName, ZONE_HECA)){
					dummyHi = quotes[QUO_TV_Z22];
				}
				else if(!_tcscmp(supZoneName, ZONE_FFDS)){
					if((mastOnCell) && (cellType == CELL_COVER_PLUG))
						dummyHi = quotes[QUO_TV_Z23_CW];
					else
						dummyHi = quotes[QUO_TV_Z23];
				}
				else if(!_tcscmp(supZoneName, ZONE_REACTOR)){
					dummyHi = quotes[QUO_TV_Z3];
				}
				else
					dummyHi = quotes[QUO_TV_TP];
				retCnd = SetTagSp(dummyLo, dummyHi, tiTVSp);	
					goto SP_MM;
			}
			else{
				retCnd = SetTagSp(TVMPos, TVMPos, tiTVSp);	
					goto SP_MM;
			}
SP_MM:
			//Main mast limitations//
			if(inC12 || inC23){
				dummyLo = 0.0;
				retCnd = SetTagSp(dummyLo, quotes[QUO_TP], tiMMSp);	
				goto SP_CLUS;
			}

			if(!mmRotCorrect || (mmFixOp && mmFixCl) || (!mmFixOp && !mmFixCl)){
				retCnd = SetTagSp(mmPos, mmPos, tiMMSp);	
				goto SP_CLUS;
			}

			mastBlocked = false;
			if(!mastOnCell){
				//Not on cell...//
				GetMMTransPos(mastSit, &dummyHi);
				dummyLo = dummyHi;
				mastBlocked = true;
				retCnd = SetTagSp(dummyLo, dummyHi, tiMMSp);
				if(retCnd != R_NORMAL)
					goto SP_CLUS;	
			}
			else{
				//On cell...//
				if(mastIsEmpty && cellIsEmpty){
					//All is empty allow movements...//
					if(!_tcscmp(supZoneName, ZONE_IO)){
						switch(contIdx){
						case Z1_NO_CONT:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							break;
						case Z1_CONT_TK13:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_TK13_FA_TM];
							break;
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_LOAD_FA_TM];
							break;
						case Z1_CONT_HECA:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							break;
						default:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							mastBlocked = true;
							break;
						}
					}
					else if(!_tcscmp(supZoneName, ZONE_FA)){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z21_FA_TM];
					}
					else if(!_tcscmp(supZoneName, ZONE_HECA)){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z22_PL_TM];
					}
					else if(!_tcscmp(supZoneName, ZONE_FFDS)){
						if(cellType == CELL_COVER_PLUG){
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z23_CLVW_PL_TM];
						}
						else if(cellType == CELL_FFDS_CASK){
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z23_FFDS_PL_TM];
						}
						else if(cellType == CELL_COVER_PLUG_EX){
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z23_CLJS_PL_TM];
						}
						else{
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z23_FFDS_PL_TM];
						}
					}
					else if(!_tcscmp(supZoneName, ZONE_REACTOR)){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z3_FA_TM];
					}
					else{
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_TP];
						mastBlocked = true;
					}
					retCnd = SetTagSp(dummyLo, dummyHi, tiMMSp);
					goto SP_CLUS;	
				}

				if(!_tcscmp(supZoneName, ZONE_IO)){
					retSt = GetCotainIdxFromCell(vx, vy, &cellContIdx);
					if(retSt != RT_NORMAL)
						goto SP_CLUS;
					if((contIdx == Z1_NO_CONT) || (cellContIdx != contIdx)){
						if(mastSit.obj[OBJT_N_FA] != 0){
							dummyHi = quotes[QUO_TP_FA];
						}
						else if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
							dummyHi = quotes[QUO_TP_CC];
						}
						else if(mastSit.obj[OBJT_N_HECA] != 0){
							dummyHi = quotes[QUO_TP_HC];
						}
						else if((mastSit.obj[OBJT_N_COVER] != 0) || (mastSit.obj[OBJT_N_FFDS_COV] != 0)){
							dummyHi = quotes[QUO_TP_HP];
						}
						else{
							dummyHi = quotes[QUO_TP];
						}
						dummyLo = dummyHi;
					}
					else if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
						switch(contIdx){
						case Z1_CONT_TK13:
							if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (((cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)) || ((cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)))){
								if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
									dummyLo = mmPos;
									dummyHi = mmPos;	
								}
								else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
									dummyLo = mmPos;
									dummyHi = mmPos;	
								}
								else{
									dummyLo = quotes[QUO_TP_CC];
									dummyHi = quotes[QUO_Z1_TK13_CJ_MM];
								}
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_TP_CC];
								mastBlocked = true;
							}
							break;
						case Z1_CONT_LOAD:
							if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (((cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)) || ((cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)))){
								if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
									dummyLo = mmPos;
									dummyHi = mmPos;	
								}
								else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
									dummyLo = mmPos;
									dummyHi = mmPos;	
								}
								else{
									dummyLo = quotes[QUO_TP_CC];
									dummyHi = quotes[QUO_Z1_LOAD_CJ_MM];	
								}
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_TP_CC];
								mastBlocked = true;
							}
							break;
						case Z1_CONT_HECA:
							dummyLo = quotes[QUO_TP_CC];
							dummyHi = quotes[QUO_TP_CC];
							mastBlocked = true;
							break;
						default:
							break;
						}
					}
					else if((mastSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_HECA] == 0)){
						switch(contIdx){
						case Z1_CONT_TK13:
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP_HC];
							dummyHi = quotes[QUO_TP_HC];
							mastBlocked = true;
							break;
						case Z1_CONT_HECA:
							dummyLo = quotes[QUO_TP_HC];
							dummyHi = quotes[QUO_Z1_HECA_HC_MM];
							break;
						default:
							dummyLo = quotes[QUO_TP_HC];
							dummyHi = quotes[QUO_TP_HC];
							mastBlocked = true;
							break;
						}
					}
					else if((mastSit.obj[OBJT_N_HECA] == 0) && (cellSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_COVER] != 0)){
						switch(contIdx){
						case Z1_CONT_TK13:
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							mastBlocked = true;
							break;
						case Z1_CONT_HECA:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_HECA_PL_TM];
							break;
						default:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							mastBlocked = true;
							break;
						}
					}
					else if((mastSit.obj[OBJT_N_FA] != 0) && ((cellSit.obj[OBJT_N_FA] == 0) && (cellSit.obj[OBJT_N_CLUS_FRM] == 0))){
						switch(contIdx){
						case Z1_CONT_TK13:
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_Z1_TK13_FA_MM];	
							break;
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_Z1_LOAD_FA_MM];
							break;
						case Z1_CONT_HECA:
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_TP_FA];
							mastBlocked = true;
							break;
						default:
							break;
						}
					}
					else if((mastSit.obj[OBJT_N_CLUS_FRM] != 0) && (cellSit.obj[OBJT_N_FA] == 0) && (cellSit.obj[OBJT_N_CLUS_FRM] == 0)){
						switch(contIdx){
						case Z1_CONT_TK13:
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_Z1_TK13_FA_MM];	//Quote not present using FA//
							break;
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_Z1_LOAD_CF_MM];
							break;
						default:
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_TP_FA];
							mastBlocked = true;
							break;
						}
					}
					else if((cellSit.obj[OBJT_N_FA] != 0) && (mastSit.obj[OBJT_N_FA] == 0) && (mastSit.obj[OBJT_N_CLUS_FRM] == 0)){
						switch(contIdx){
						case Z1_CONT_TK13:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_TK13_FA_TM];	
							break;
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_LOAD_FA_TM];
							break;
						default:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							mastBlocked = true;
							break;
						}
					}
					else if((cellSit.obj[OBJT_N_CLUS_FRM] != 0) && (mastSit.obj[OBJT_N_FA] == 0) && (mastSit.obj[OBJT_N_CLUS_FRM] == 0)){
						switch(contIdx){
						case Z1_CONT_TK13:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_TK13_FA_TM];	
							break;
						case Z1_CONT_LOAD:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z1_LOAD_CF_TM];
							break;
						default:
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_TP];
							mastBlocked = true;
							break;
						}
					}
					else{
						GetMMTransPos(mastSit, &dummyHi);
						dummyLo = dummyHi;
						mastBlocked = true;
					}
				}
				else if(!_tcscmp(supZoneName, ZONE_FA)){
					if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
						if(((cellSit.obj[OBJT_N_FA] != 0) || ((cellSit.obj[OBJT_N_CLUS_FRM] != 0))) && ((cellSit.obj[OBJT_N_CLUS] != 0) || (mastSit.obj[OBJT_N_CLUS] != 0))){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z21_CJ_MM];
							}
						}
						else{
							dummyLo = quotes[QUO_TP_CC];
							dummyHi = quotes[QUO_TP_CC];
							mastBlocked = true;
						}
					}
					else if((mastSit.obj[OBJT_N_FA] != 0) && cellIsEmpty){
						dummyLo = quotes[QUO_TP_FA];
						dummyHi = quotes[QUO_Z21_FA_MM];
					}
					else if((mastSit.obj[OBJT_N_CLUS_FRM] != 0) && cellIsEmpty){
						dummyLo = quotes[QUO_TP_FA];
						dummyHi = quotes[QUO_Z21_CF_MM];
					}
					else if((cellSit.obj[OBJT_N_FA] != 0) && mastIsEmpty){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z21_FA_TM];
					}
					else if((cellSit.obj[OBJT_N_CLUS_FRM] != 0) && mastIsEmpty){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z21_CF_TM];
					}
					else{
						GetMMTransPos(mastSit, &dummyLo);
						dummyHi = dummyLo;
						mastBlocked = true;
					}
				}
				else if(!_tcscmp(supZoneName, ZONE_HECA)){
					if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
						if((mastSit.obj[OBJT_N_CLUS] != 0) && (cellSit.obj[OBJT_N_CLUS] == 0) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_FA] != 0)){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z22_CJ_MM];
							}
						}
						else if((mastSit.obj[OBJT_N_CLUS] == 0) && (cellSit.obj[OBJT_N_CLUS] != 0) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_FA] != 0)){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z22_CJ_MM];
							}
						}
						else{
							GetMMTransPos(mastSit, &dummyLo);
							dummyHi = dummyLo;
							mastBlocked = true;						
						}
					}
					else if((mastSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_HECA] == 0)){
						dummyLo = quotes[QUO_TP_HC];
						dummyHi = quotes[QUO_Z22_HC_MM];
					}
					else if((cellSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_COVER] != 0) && mastIsEmpty){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z22_PL_TM];
					}
					else if((mastSit.obj[OBJT_N_COVER] != 0) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_HECA] != 0)){
						dummyLo = quotes[QUO_TP_HP];
						dummyHi = quotes[QUO_Z22_PL_MM];
					}
					else if((cellSit.obj[OBJT_N_COVER] != 0) && mastIsEmpty){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z22_PL_TM];
					}
					else if((mastSit.obj[OBJT_N_FA] != 0) && (cellSit.obj[OBJT_N_HECA] != 0) && (cellSit.obj[OBJT_N_COVER] == 0)){
						dummyLo = quotes[QUO_TP_FA];
						dummyHi = quotes[QUO_Z22_FA_MM];
					}
					else if((cellSit.obj[OBJT_N_FA] != 0) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_HECA] != 0) && mastIsEmpty){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z22_FA_TM];
					}
					else{
						GetMMTransPos(mastSit, &dummyLo);
						dummyHi = dummyLo;
						mastBlocked = true;
					}
				}
				else if(!_tcscmp(supZoneName, ZONE_FFDS)){
					switch(cellType){
					case CELL_COVER_PLUG:		
						//Plug seat and cluster viewing//	
						if((mastSit.obj[OBJT_N_CLUS_CAS] != 0)  && (cellSit.obj[OBJT_N_COVER] == 0)  && (cellSit.obj[OBJT_N_FFDS_COV] == 0) && (cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z23_CLVW_CJ_MM];
							}
						}
						else if((mastSit.obj[OBJT_N_CLUS_CAS] != 0)  && (cellSit.obj[OBJT_N_COVER] == 0)  && (cellSit.obj[OBJT_N_FFDS_COV] == 0) && (cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z23_CLVW_CJ_MM];
							}
						}
						else if(((mastSit.obj[OBJT_N_COVER] != 0) || (mastSit.obj[OBJT_N_FFDS_COV] != 0)) && ((cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0))){
							if((mmRot >= (float)(45.0 - rotToll)) && (mmRot <= (float)(45.0 + rotToll))){
								dummyLo = quotes[QUO_TP_HP];
								dummyHi = quotes[QUO_Z23_CLVW_PL_MM];
							}
							else{
								dummyLo = mmPos;
								dummyHi = mmPos;
							}
						}
						else if(((cellSit.obj[OBJT_N_COVER] != 0) || (cellSit.obj[OBJT_N_FFDS_COV] != 0)) && mastIsEmpty){
							if((mmRot >= (float)(0.0 - rotToll)) && (mmRot <= (float)(0.0 + rotToll))){
								dummyLo = quotes[QUO_TP];
								dummyHi = quotes[QUO_Z23_CLVW_PL_TM];
							}
							else{
								dummyLo = mmPos;
								dummyHi = mmPos;
							}
						}
						else{
							GetMMTransPos(mastSit, &dummyLo);
							dummyHi = dummyLo;
							mastBlocked = true;
						}
						break;
					case CELL_FFDS_CASK:
						if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
							GetMMTransPos(mastSit, &dummyLo);
							dummyHi = dummyLo;
							mastBlocked = true;
						}
						else if((mastSit.obj[OBJT_N_FFDS_COV] != 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0)){
							if((mmRot >= (float)(45.0 - rotToll)) && (mmRot <= (float)(45.0 + rotToll))){
								dummyLo = quotes[QUO_TP_HP];
								dummyHi = quotes[QUO_Z23_FFDS_PL_MM];
							}
							else{
								dummyLo = mmPos;
								dummyHi = mmPos;
							}
						}
						else if((cellSit.obj[OBJT_N_FFDS_COV] != 0) && mastIsEmpty){
							if((mmRot >= (float)(0.0 - rotToll)) && (mmRot <= (float)(0.0 + rotToll))){
								dummyLo = quotes[QUO_TP];
								dummyHi = quotes[QUO_Z23_FFDS_PL_TM];
							}
							else{
								dummyLo = mmPos;
								dummyHi = mmPos;
							}
						}
						else if((mastSit.obj[OBJT_N_FA] != 0) && cellIsEmpty){
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_Z23_FFDS_FA_MM];
						}
						else if((cellSit.obj[OBJT_N_FA] != 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0) && mastIsEmpty){
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z23_FFDS_FA_TM];
						}
						else{
							GetMMTransPos(mastSit, &dummyLo);
							dummyHi = dummyLo;
							mastBlocked = true;
						}
						break;
					case CELL_COVER_PLUG_EX:
						if((mastSit.obj[OBJT_N_CLUS_CAS] != 0)  && (cellSit.obj[OBJT_N_COVER] == 0)  && (cellSit.obj[OBJT_N_FFDS_COV] == 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
							dummyLo = quotes[QUO_TP_CC];
							dummyHi = quotes[QUO_Z23_CLJS_CJ_MM];
						}
						else if((cellSit.obj[OBJT_N_CLUS_CAS] != 0) && (cellSit.obj[OBJT_N_COVER] == 0)  && (cellSit.obj[OBJT_N_FFDS_COV] == 0) && mastIsEmpty){
							dummyLo = quotes[QUO_TP];
							dummyHi = quotes[QUO_Z23_CLJS_CJ_TM];
						}
						else if(((mastSit.obj[OBJT_N_COVER] != 0) || (mastSit.obj[OBJT_N_FFDS_COV] != 0)) && ((cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0))){
							if((mmRot >= (float)(45.0 - rotToll)) && (mmRot <= (float)(45.0 + rotToll))){
								dummyLo = quotes[QUO_TP_HP];
								dummyHi = quotes[QUO_Z23_CLJS_PL_MM];
							}
							else{
								dummyLo = mmPos;
								dummyHi = mmPos;
							}
						}
						else if(((cellSit.obj[OBJT_N_COVER] != 0) || (cellSit.obj[OBJT_N_FFDS_COV] != 0)) && mastIsEmpty){
							if((mmRot >= (float)(0.0 - rotToll)) && (mmRot <= (float)(0.0 + rotToll))){
								dummyLo = quotes[QUO_TP];
								dummyHi = quotes[QUO_Z23_CLJS_PL_TM];
							}
							else{
								dummyLo = mmPos;
								dummyHi = mmPos;
							}
						}
						else{
							GetMMTransPos(mastSit, &dummyLo);
							dummyHi = dummyLo;
							mastBlocked = true;
						}
						break;
					default:
						GetMMTransPos(mastSit, &dummyLo);
						dummyHi = dummyLo;
						mastBlocked = true;
						break;
					}
				}
				else if(!_tcscmp(supZoneName, ZONE_REACTOR)){
					if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
						if((cellSit.obj[OBJT_N_FA] != 0) && (cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z3_CJ_MM];
							}
						}
						else if((cellSit.obj[OBJT_N_FA] != 0) && (cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)){
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL_CLUS] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (mmClPos > (float)(mmPos + quotes[QUO_TP_CL] + TOLL_MM))){
								dummyLo = mmPos;
								dummyHi = mmPos;	
							}
							else{
								dummyLo = quotes[QUO_TP_CC];
								dummyHi = quotes[QUO_Z3_CJ_MM];
							}
						}
						else{
							GetMMTransPos(mastSit, &dummyLo);
							dummyHi = dummyLo;
							mastBlocked = true;
						}
					}
					else if((mastSit.obj[OBJT_N_FA] != 0) && cellIsEmpty){
						if(!mmDisplDown){
							dummyLo = quotes[QUO_Z3_FA_MM];
							dummyHi = quotes[QUO_Z3_FA_MM] + (float)(100.0);
						}else{
							dummyLo = quotes[QUO_TP_FA];
							dummyHi = quotes[QUO_Z3_FA_MM];
						}
					}
					else if((cellSit.obj[OBJT_N_FA] != 0) && mastIsEmpty){
						dummyLo = quotes[QUO_TP];
						dummyHi = quotes[QUO_Z3_FA_TM];
					}
					else{
						GetMMTransPos(mastSit, &dummyLo);
						dummyHi = dummyLo;
						mastBlocked = true;
					}
				}
				else{
					GetMMTransPos(mastSit, &dummyLo);
					dummyHi = dummyLo;
					mastBlocked = true;
				}
				retCnd = SetTagSp(dummyLo, dummyHi, tiMMSp);
				if(retCnd != R_NORMAL)
					goto SP_CLUS;			
			}
SP_CLUS:
			//Main mast cluster limitations//
		
			//In corridors or as fixator//
			if(inC12 || inC23 || (mastSit.obj[OBJT_N_CLUS_CAS] == 0)){
				if(mmFixCl && mmFixOp){
					retCnd = SetTagSp(mmClPos, mmClPos, tiMMClusSp);	
					goto MM_ROT;
				}

				if(mmFixCl && !IsPerm(tiMMUnfixReq)){
					retCnd = SetTagSp(mmClPos, mmClPos, tiMMClusSp);	
					goto MM_ROT;
				}

				if(mmFixOp && !IsPerm(tiMMFixReq)){
					retCnd = SetTagSp(mmClPos, mmClPos, tiMMClusSp);	
					goto MM_ROT;
				}

				retSt = GetMMFixSP(&dummyLo, &dummyHi);
				if(retSt != RT_NORMAL)
					goto MM_ROT;

				retCnd = SetTagSp(dummyLo, dummyHi, tiMMClusSp);	
				goto MM_ROT;
			}

			if(!mmRotCorrect || !(mmFixCl && !mmFixOp) || falvlMount || dimpMount || !touchDown){
				retCnd = SetTagSp(mmClPos, mmClPos, tiMMClusSp);	
				goto MM_ROT;
			}

			if(mastSit.obj[OBJT_N_CLUS_CAS] != 0){
				//As cluster gripper//
				if((!mastOnCell) || mastBlocked){
					//Not on cell...//
					if(mastSit.obj[OBJT_N_CLUS] == 0)
						dummyHi = mmPos + quotes[QUO_TP_CL];
					else
						dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
					dummyLo = dummyHi;
					retCnd = SetTagSp(dummyLo, dummyHi, tiMMClusSp);	
					goto MM_ROT;
				}
				else{
					//On cell...//
					if(!_tcscmp(supZoneName, ZONE_IO)){
						retSt = GetCotainIdxFromCell(vx, vy, &cellContIdx);
						if(retSt != RT_NORMAL)
							goto MM_ROT;

						if((contIdx != cellContIdx) || (contIdx == Z1_CONT_HECA) || (contIdx == Z1_NO_CONT)){
							if(mastSit.obj[OBJT_N_CLUS] == 0)
								dummyHi = mmPos + quotes[QUO_TP_CL];
							else
								dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
							dummyLo = dummyHi;
							retCnd = SetTagSp(dummyLo, dummyHi, tiMMClusSp);	
							goto MM_ROT;
						}
						else{
							if(contIdx == Z1_CONT_LOAD){
								if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
									dummyLo = mmPos + quotes[QUO_TP_CL];
									dummyHi = quotes[QUO_EXP_CL_Z1_L];
								}
								else if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)){
									dummyLo = mmPos + quotes[QUO_TP_CL_CLUS];
									dummyHi = quotes[QUO_EXP_CL_Z1_L];
								}
								else{
									if(mastSit.obj[OBJT_N_CLUS] == 0)
										dummyHi = mmPos + quotes[QUO_TP_CL];
									else
										dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
									dummyLo = dummyHi;
								}
							}
							else if(contIdx == Z1_CONT_TK13){
								if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
									dummyLo = mmPos + quotes[QUO_TP_CL];
									dummyHi = quotes[QUO_EXP_CL_Z1_U];
								}
								else if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)){
									dummyLo = mmPos + quotes[QUO_TP_CL_CLUS];
									dummyHi = quotes[QUO_EXP_CL_Z1_U];
								}
								else{
									if(mastSit.obj[OBJT_N_CLUS] == 0)
										dummyHi = mmPos + quotes[QUO_TP_CL];
									else
										dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
									dummyLo = dummyHi;
								}
							}
							else{
								if(mastSit.obj[OBJT_N_CLUS] == 0)
									dummyHi = mmPos + quotes[QUO_TP_CL];
								else
									dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
								dummyLo = dummyHi;
							}
						}
					}
					else if(!_tcscmp(supZoneName, ZONE_FA)){
						if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
							dummyLo = mmPos + quotes[QUO_TP_CL];
							dummyHi = quotes[QUO_EXP_CL_Z21];
						}
						else if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)){
							dummyLo = mmPos + quotes[QUO_TP_CL_CLUS];
							dummyHi = quotes[QUO_EXP_CL_Z21];
						}
						else{
							if(mastSit.obj[OBJT_N_CLUS] == 0)
								dummyHi = mmPos + quotes[QUO_TP_CL];
							else
								dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
							dummyLo = dummyHi;
						}
					}
					else if(!_tcscmp(supZoneName, ZONE_HECA)){
						if(mastSit.obj[OBJT_N_CLUS] == 0)
							dummyHi = mmPos + quotes[QUO_TP_CL];
						else
							dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
						dummyLo = dummyHi;
					}
					else if(!_tcscmp(supZoneName, ZONE_FFDS)){
						switch(cellType){
						case CELL_COVER_PLUG:
							//Cluster viewing//
							if((mastSit.obj[OBJT_N_CLUS] != 0) && (cellSit.obj[OBJT_N_CLUS] == 0) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0)){
								dummyLo = mmPos + quotes[QUO_TP_CL_CLUS];
								dummyHi = quotes[QUO_EXP_CL_Z23];
							}
							else if((mastSit.obj[OBJT_N_CLUS] == 0) && (cellSit.obj[OBJT_N_CLUS] != 0) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0)){
								dummyLo = mmPos + quotes[QUO_TP_CL];
								dummyHi = quotes[QUO_EXP_CL_Z23];
							}
							else{
								if(mastSit.obj[OBJT_N_CLUS] == 0)
									dummyHi = mmPos + quotes[QUO_TP_CL];
								else
									dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
								dummyLo = dummyHi;
							}
							break;
						case CELL_COVER_PLUG_EX:
							//Cluster casing home//
							if((mastSit.obj[OBJT_N_CLUS] == 0)){
								retSt = GetMMFixSP(&temp1, &dummyLo);
								if(retSt != RT_NORMAL)
									goto MM_ROT;
								dummyHi = mmPos + quotes[QUO_TP_CL];
							}
							else{
								if(mastSit.obj[OBJT_N_CLUS] != 0)
									dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
								else
									dummyHi = mmPos + quotes[QUO_TP_CL];
								dummyLo = dummyHi;
							}
							break;
						default:
							if(mastSit.obj[OBJT_N_CLUS] == 0)
								dummyHi = mmPos + quotes[QUO_TP_CL];
							else
								dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
							dummyLo = dummyHi;
							break;
						}
					}
					else if(!_tcscmp(supZoneName, ZONE_REACTOR)){
						if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] != 0) && (mastSit.obj[OBJT_N_CLUS] == 0)){
							dummyLo = mmPos + quotes[QUO_TP_CL];
							dummyHi = quotes[QUO_EXP_CL_Z3];
						}
						else if(((cellSit.obj[OBJT_N_FA] != 0) || (cellSit.obj[OBJT_N_CLUS_FRM] != 0)) && (cellSit.obj[OBJT_N_CLUS] == 0) && (mastSit.obj[OBJT_N_CLUS] != 0)){
							dummyLo = mmPos + quotes[QUO_TP_CL_CLUS];
							dummyHi = quotes[QUO_EXP_CL_Z3];
						}
						else{
							if(mastSit.obj[OBJT_N_CLUS] == 0)
								dummyHi = mmPos + quotes[QUO_TP_CL];
							else
								dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
							dummyLo = dummyHi;
						}
					}
					else{
						if(mastSit.obj[OBJT_N_CLUS] == 0)
							dummyHi = mmPos + quotes[QUO_TP_CL];
						else
							dummyHi = mmPos + quotes[QUO_TP_CL_CLUS];
						dummyLo = dummyHi;
					}

					retCnd = SetTagSp(dummyLo, dummyHi, tiMMClusSp);	
					goto MM_ROT;
				}
			}
MM_ROT:
			//MAIN MAST ROTATION//
			/*
			if(mastOnCell){
				if(mmFixCl && !mmFixOp && touchDown && ((mastSit.obj[OBJT_N_COVER] != 0) || (mastSit.obj[OBJT_N_FFDS_COV] != 0)) && ((!_tcscmp(supZoneName, _T("Z22"))) || (!_tcscmp(supZoneName, _T("Z23"))))){
					dummyLo = (float)(0.0 - rotToll);
					dummyHi = (float)(45.0 + rotToll);
				}
				else if(mmTransp && ((mastSit.obj[OBJT_N_COVER] != 0) || (mastSit.obj[OBJT_N_FFDS_COV] != 0)) && (cellSit.obj[OBJT_N_COVER] == 0) && (cellSit.obj[OBJT_N_FFDS_COV] == 0) && ((!_tcscmp(supZoneName, _T("Z22"))) || (!_tcscmp(supZoneName, _T("Z23"))))){
					dummyLo = (float)(45.0 - rotToll);
					dummyHi = (float)(45.0 + rotToll);
				}
				else if(mmTransp && ((cellSit.obj[OBJT_N_COVER] != 0) || (cellSit.obj[OBJT_N_FFDS_COV] != 0)) && mastIsEmpty && ((!_tcscmp(supZoneName, _T("Z22"))) || (!_tcscmp(supZoneName, _T("Z23"))))){
					dummyLo = (float)(0.0 - rotToll);
					dummyHi = (float)(0.0 + rotToll);
				}
				else{
					dummyLo = mmRot;
					dummyHi = mmRot;
				}
				retCnd = SetTagSp(dummyLo, dummyHi, tiMMRotSp);
			}
			else{
				dummyLo = mmRot;
				dummyHi = mmRot;
				retCnd = SetTagSp(dummyLo, dummyHi, tiMMRotSp);
			}
			*/
			if(mmRotFree){
				dummyLo = (float)(0.0 - rotToll);
				dummyHi = (float)(45.0 + rotToll);
			}
			else{
				dummyLo = mmRot;
				dummyHi = mmRot;
			}
			retCnd = SetTagSp(dummyLo, dummyHi, tiMMRotSp);

SND_MSG:
			//if(manHwnd == 0)
			manHwnd = FindWindow(NULL, _T("Manual Mode Management"));
			SendMessage(manHwnd, WM_CHNG_SP, 0, 0);
		}
SLEEP:
	Sleep(cycleTime);	
	}
	

	return(RT_NORMAL);
}

RETSTAT LoadParam(){
	RETSTAT			retSt;
	int				i;
	int				pcu;
	float			val;
	
	retSt = GetPcuNum(&pcu);
	for(i = 0; i < NUM_POINT; i++){
		switch(i){
		case 0:
			retSt = GetInfiBlockValEx(8324, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8324 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // - (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8330, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8330 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 1:
			retSt = GetInfiBlockValEx(8325, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8325 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8330, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8330 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 2:
			retSt = GetInfiBlockValEx(8325, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8325 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8331, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8331 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val; // - (float)(TOLL_MM);
			break;
		case 3:
			retSt = GetInfiBlockValEx(8326, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8326 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // - (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8331, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8331 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val; // - (float)(TOLL_MM);
			break;
		case 4:
			retSt = GetInfiBlockValEx(8326, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8326 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // - (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8332, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8332 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 5:
			retSt = GetInfiBlockValEx(8327, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8327 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8332, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8332 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 6:
			retSt = GetInfiBlockValEx(8327, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8327 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8333, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8333 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 7:
			retSt = GetInfiBlockValEx(8326, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8326 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val;
			retSt = GetInfiBlockValEx(8333, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8333 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 8:
			retSt = GetInfiBlockValEx(8326, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8326 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // - (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8334, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8334 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 9:
			retSt = GetInfiBlockValEx(8328, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8328 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // - (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8334, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8334 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val; // + (float)(TOLL_MM);
			break;
		case 10:
			retSt = GetInfiBlockValEx(8328, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8328 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8335, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8335 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val; // - (float)(TOLL_MM);
			break;
		case 11:
			retSt = GetInfiBlockValEx(8329, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8329 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8335, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8335 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val; // - (float)(TOLL_MM);
			break;
		case 12:
			retSt = GetInfiBlockValEx(8325, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8325 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8336, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8336 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val; // - (float)(TOLL_MM);
			break;
		case 13:
			retSt = GetInfiBlockValEx(8325, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8325 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // + (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8337, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8337 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		case 14:
			retSt = GetInfiBlockValEx(8324, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8324 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].x = val; // - (float)(TOLL_MM);
			retSt = GetInfiBlockValEx(8337, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8337 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			perim[i].y = val;
			break;
		default:
			break;
		}
	}

	for(i = 0; i < NUM_SPS_QUOTES; i++){
		switch(i){
		case QUO_TP:
			retSt = GetInfiBlockValEx(8621, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8621 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case  QUO_TP_FA:
			retSt = GetInfiBlockValEx(8622, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8622 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TP_CC:
			retSt = GetInfiBlockValEx(8620, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8620 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TP_HC:
			retSt = GetInfiBlockValEx(8623, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8623 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TP_HP:
			retSt = GetInfiBlockValEx(8624, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8624 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;		
			break;
		case QUO_TV_TP:
			retSt = GetInfiBlockValEx(8320, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block  8320 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TV_Z1:
			retSt = GetInfiBlockValEx(8525, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8525 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TV_Z21:
			retSt = GetInfiBlockValEx(8526, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8526 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TV_Z23:
			retSt = GetInfiBlockValEx(8527, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8527 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TV_Z3:
			retSt = GetInfiBlockValEx(8528, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8528 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TP_CL:
			retSt = GetInfiBlockValEx(8529, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 7319 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TP_CL_CLUS	:
			retSt = GetInfiBlockValEx(8626, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8626 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_EXP_CL_Z1_L:
			retSt = GetInfiBlockValEx(8618, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8618 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_EXP_CL_Z1_U:
			retSt = GetInfiBlockValEx(8720, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8720 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_EXP_CL_Z21:
			retSt = GetInfiBlockValEx(3483, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 3483 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_EXP_CL_Z23:
			retSt = GetInfiBlockValEx(8722, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8722 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_EXP_CL_Z3:
			retSt = GetInfiBlockValEx(3484, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 3484 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_LOAD_FA_TM:
			retSt = GetInfiBlockValEx(8770, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8770 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_LOAD_CF_TM:
			retSt = GetInfiBlockValEx(8771, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8771 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_LOAD_FA_MM:
			retSt = GetInfiBlockValEx(8772, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8772 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_LOAD_CJ_MM:
			retSt = GetInfiBlockValEx(8773, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8773 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_LOAD_CF_MM:
			retSt = GetInfiBlockValEx(8774, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8774 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_TK13_FA_TM:
			retSt = GetInfiBlockValEx(8775, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8775 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_TK13_FA_MM:
			retSt = GetInfiBlockValEx(8776, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8776 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_TK13_CJ_MM:
			retSt = GetInfiBlockValEx(8777, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8777 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_HECA_PL_TM:
			retSt = GetInfiBlockValEx(8778, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8778 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z1_HECA_HC_MM:
			retSt = GetInfiBlockValEx(8779, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8779 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z21_FA_TM:
			retSt = GetInfiBlockValEx(8780, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8780 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z21_CF_TM:
			retSt = GetInfiBlockValEx(8781, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8781 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z21_FA_MM:
			retSt = GetInfiBlockValEx(8782, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8782 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z21_CJ_MM:
			retSt = GetInfiBlockValEx(8783, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8783 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z21_CF_MM:
			retSt = GetInfiBlockValEx(8784, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8784 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_FA_TM:
			retSt = GetInfiBlockValEx(8785, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8785 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_HC_TM:
			retSt = GetInfiBlockValEx(8786, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8786 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_PL_TM:
			retSt = GetInfiBlockValEx(8787, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8787 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_FA_MM:
			retSt = GetInfiBlockValEx(8788, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8788 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_CJ_MM:
			retSt = GetInfiBlockValEx(8789, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8789 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_HC_MM:
			retSt = GetInfiBlockValEx(8790, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8790 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z22_PL_MM:
			retSt = GetInfiBlockValEx(8791, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8791 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_FFDS_FA_TM:
			retSt = GetInfiBlockValEx(8792, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8792 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_FFDS_PL_TM:
			retSt = GetInfiBlockValEx(8793, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8793 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_FFDS_FA_MM:
			retSt = GetInfiBlockValEx(8794, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8794 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_FFDS_PL_MM:
			retSt = GetInfiBlockValEx(8795, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8795 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLJS_CJ_TM:
			retSt = GetInfiBlockValEx(8796, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8796 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLJS_PL_TM:
			retSt = GetInfiBlockValEx(8797, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8797 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLJS_CJ_MM:
			retSt = GetInfiBlockValEx(8798, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8798 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLJS_PL_MM:
			retSt = GetInfiBlockValEx(8799, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8799 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLVW_PL_TM:
			retSt = GetInfiBlockValEx(8800, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8800 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLVW_CJ_MM:
			retSt = GetInfiBlockValEx(8801, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8801 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z23_CLVW_PL_MM:
			retSt = GetInfiBlockValEx(8802, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8802 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z3_FA_TM:
			retSt = GetInfiBlockValEx(8803, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8803 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z3_FA_MM:
			retSt = GetInfiBlockValEx(8804, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8804 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_Z3_CJ_MM:
			retSt = GetInfiBlockValEx(8805, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8805 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TV_Z23_CW:
			retSt = GetInfiBlockValEx(8546, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8546 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		case QUO_TV_Z22:
			retSt = GetInfiBlockValEx(8530, 0, TUNE_FROM_DAFS, &val);
			if(retSt != RT_NORMAL){
				MessageBox(NULL, _T("Error loading block 8530 value."), _T("Error"), MB_OK);
				return(retSt);
			}
			quotes[i] = val;
			break;
		default:
			break;	
		}		
	}

	retSt = GetInfiBlockValEx(8347, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8347 value."), _T("Error"), MB_OK);
		return(retSt);
	}
	
	react.center.x = val;

	retSt = GetInfiBlockValEx(8348, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8348 value."), _T("Error"), MB_OK);
		return(retSt);
	}
	
	react.center.y = val;

	retSt = GetInfiBlockValEx(8349, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8349 value."), _T("Error"), MB_OK);
		return(retSt);
	}
	
	react.radius = val;

	retSt = GetInfiBlockValEx(8342, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8342 value."), _T("Error"), MB_OK);
		return(retSt);
	}
	
	cont.center.x = val;

	retSt = GetInfiBlockValEx(8343, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8343 value."), _T("Error"), MB_OK);
		return(retSt);
	}
	
	cont.center.y = val;

	retSt = GetInfiBlockValEx(8344, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8344 value."), _T("Error"), MB_OK);
		return(retSt);
	}
	
	cont.radius = val;

	retSt = GetInfiBlockValEx(8218, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8218 value."), _T("Error"), MB_OK);
		return(retSt);
	}

	C12Axis = val;

	retSt = GetInfiBlockValEx(8220, 0, TUNE_FROM_DAFS, &val);
	if(retSt != RT_NORMAL){
		MessageBox(NULL, _T("Error loading block  8220 value."), _T("Error"), MB_OK);
		return(retSt);
	}

	C23Axis = val;

	return(RT_NORMAL);
}

RETSTAT CheckMov(TAGIDX tiStart, bool *flag){
	RETCOND			retCnd;
	TAGVALEX		tagVal;

	*flag = false;

	retCnd = TagRead(&tiStart, &tagVal);
	if(retCnd != R_NORMAL)
		return(RT_FAILED);

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		*flag = true;
	else
		*flag = false;

	return(RT_NORMAL);
}

RETSTAT GetZ23X(physPoint p1, physPoint p2, float y, float *x){
	*x = (float)((((p2.x - p1.x)/(p2.y - p1.y)) * (y - p1.y)) + p1.x);

	return(RT_NORMAL);
}

RETSTAT GetZ23Y(physPoint p1, physPoint p2, float x, float *y){
	*y = (float)((((p2.y - p1.y)/(p2.x - p1.x)) * (x - p1.x)) + p1.y);

	return(RT_NORMAL);
}

RETSTAT GetCircX12(physPoint center, float radius, float y, float *x1, float *x2){
	double		a;
	double		b;
	double		c;
	double		delta;
	float		sol_1;
	float		sol_2;

	a = 1.0;
	b = (double)((-2) * (center.x));
	c = (double)(pow((double)(center.x), 2) + pow((double)(y), 2) - (double)(2 * (double)(y) * (double)(center.y)) + pow((double)(center.y), 2) - pow((double)(radius), 2));

	if(((pow(b, 2) - (double)(4 * a * c))) < 0){
		*x1 = -1.0;
		*x2 = -1.0;	
		return(RT_NORMAL);
	}

	delta = sqrt((pow(b, 2) - (double)(4 * a * c)));

	sol_1 = (float)((((-1) * b) - delta) / 2);
	sol_2 = (float)((((-1) * b) + delta) / 2);
	
	if(sol_1 < sol_2){
		*x1 = sol_1;
		*x2 = sol_2;	
	}
	else{
		*x1 = sol_2;
		*x2 = sol_1;	
	}

	return(RT_NORMAL);
}

RETSTAT GetCircY12(physPoint center, float radius, float x, float *y1, float *y2){
	float		a;
	float		b;
	float		c;
	float		delta;
	float		sol_1;
	float		sol_2;

	a = 1.0;
	b = (float)((-2) * (center.y));
	c = (float)((float)(pow(center.y, 2)) + (float)(pow(x, 2)) - (float)(2 * x * center.x) + (float)(pow(center.x, 2)) - (float)(pow(radius, 2)));

	if(((pow(b, 2) - (double)(4 * a * c))) < 0){
		*y1 = -1.0;
		*y2 = -1.0;	
		return(RT_NORMAL);
	}

	delta = (float)(sqrt(((double)(pow(b, 2)) - (double)(4 * a * c))));

	sol_1 = (float)((((-1) * b) - delta) / 2);
	sol_2 = (float)((((-1) * b) + delta) / 2);
	
	if(sol_1 < sol_2){
		*y1 = sol_1;
		*y2 = sol_2;	
	}
	else{
		*y1 = sol_2;
		*y2 = sol_1;	
	}

	return(RT_NORMAL);
	return(RT_NORMAL);
}

RETSTAT SetDefaultLimits(){
	RETCOND			retCnd;
	TAGIDX			tagIdxSp;
	TAGIDX			tagIdxPos;
	TAGINFO			tagInfoSp;
	TAGINFO			tagInfoPos;
	
	//Bridge default sp limits//
	retCnd = GetTagIdx(TWN_BRIDGE_POS, &tagIdxPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_BRIDGE_SP, &tagIdxSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_SP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagInfo(&tagIdxPos, DB_VAL0, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL0, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL0, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_BRIDGE_SP DB_VAL0"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_SP DB_VAL0"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_POS DB_VAL0"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagInfo(&tagIdxPos, DB_VAL100, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL100, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL100, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_BRIDGE_SP DB_VAL100"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_SP DB_VAL100"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_POS DB_VAL100"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	//Trolley default sp limits//
	retCnd = GetTagIdx(TWN_TROLLEY_POS, &tagIdxPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TROLLEY_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_TROLLEY_SP, &tagIdxSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TROLLEY_SP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	retCnd = GetTagInfo(&tagIdxPos, DB_VAL0, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL0, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL0, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_TROLLEY_SP DB_VAL0"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_TROLLEY_SP DB_VAL0"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_POS idx DB_VAL0"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagInfo(&tagIdxPos, DB_VAL100, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL100, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL100, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_TROLLEY_SP DB_VAL100"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_TROLLEY_SP DB_VAL100"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_BRIDGE_POS idx DB_VAL100"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	//TV Mast rotation default sp limits//
	retCnd = GetTagIdx(TWN_TV_ROT_POS, &tagIdxPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TV_ROT_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	retCnd = GetTagIdx(TWN_TV_ROT_SP, &tagIdxSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TV_ROT_SP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagInfo(&tagIdxPos, DB_VAL0, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL0, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL0, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_TV_ROT_SP DB_VAL0"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_TV_ROT_SP DB_VAL0"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_TV_ROT_POS DB_VAL0"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagInfo(&tagIdxPos, DB_VAL100, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL100, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL100, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error Setting tag TWN_TV_ROT_SP DB_VAL100"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_TV_ROT_SP DB_VAL100"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_TV_ROT_POS DB_VAL100"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	//Main Mast default sp limits//
	retCnd = GetTagIdx(TWN_FAGR_POS, &tagIdxPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_FAGR_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_FAGR_SP, &tagIdxSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_FAGR_SP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagInfo(&tagIdxPos, DB_VAL0, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL0, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL0, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_FAGR_SP DB_VAL0"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_FAGR_SP DB_VAL0"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_FAGR_POS DB_VAL0"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagInfo(&tagIdxPos, DB_VAL100, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL100, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL100, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_FAGR_SP DB_VAL100"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_FAGR_SP DB_VAL100"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_FAGR_POS DB_VAL100"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	//TV Mast default sp limits//
	retCnd = GetTagIdx(TWN_TVM_POS, &tagIdxPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TVM_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_TVM_SP, &tagIdxSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_TVM_SP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagInfo(&tagIdxPos, DB_VAL0, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL0, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL0, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_TVM_SP DB_VAL0"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_TVM_SP DB_VAL0"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_TVM_POS DB_VAL0"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagInfo(&tagIdxPos, DB_VAL100, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL100, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL100, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_TVM_SP DB_VAL100"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_TVM_SP DB_VAL100"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_TVM_POS DB_VAL100"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	//MM Cluster//
	retCnd = GetTagIdx(TWN_CLGR_POS, &tagIdxPos);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_CLGR_POS idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagIdx(TWN_CLGR_SP, &tagIdxSp);
	if(retCnd != R_NORMAL){
		MessageBox(NULL, _T("Error getting tag TWN_CLGR_SP idx"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	retCnd = GetTagInfo(&tagIdxPos, DB_VAL0, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL0, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL0, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_CLGR_SP DB_VAL0"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_CLGR_SP DB_VAL0"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_CLGR_POS DB_VAL0"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}
	
	retCnd = GetTagInfo(&tagIdxPos, DB_VAL100, &tagInfoPos);
	if(retCnd == R_NORMAL){
		retCnd = GetTagInfo(&tagIdxSp, DB_VAL100, &tagInfoSp);
		if(retCnd == R_NORMAL){
			tagInfoSp.rValue = tagInfoPos.rValue;
			retCnd = SetTagInfo (&tagIdxSp, DB_VAL100, &tagInfoSp, 0);
			if(retCnd != R_NORMAL){
				MessageBox(NULL, _T("Error setting tag TWN_CLGR_SP DB_VAL100"), _T("Error"), MB_OK);
				return(RT_FAILED);
			}
		}
		else{
			MessageBox(NULL, _T("Error getting tag TWN_CLGR_SP DB_VAL100"), _T("Error"), MB_OK);
			return(RT_FAILED);
		}
	}
	else{
		MessageBox(NULL, _T("Error getting tag TWN_CLGR_POS DB_VAL100"), _T("Error"), MB_OK);
		return(RT_FAILED);
	}

	return(RT_NORMAL);
}

RETSTAT InsideCircle(physPoint center, float radius, float x, float y, bool *inside){
	float		temp;

	temp = ((x - center.x) * (x - center.x)) + ((y - center.y) * (y - center.y));

	if(temp <= (radius * radius))
		*inside = true;
	else
		*inside = false;

	return(RT_NORMAL);
}

RETSTAT GetMMTransPos(OBJTMM mastSit, float *pos){
	if(mastSit.obj[OBJT_N_FA] != 0)
		*pos = quotes[QUO_TP_FA];
	else if(mastSit.obj[OBJT_N_CLUS_CAS] != 0)
		*pos = quotes[QUO_TP_CC];
	else if(mastSit.obj[OBJT_N_HECA] != 0)
		*pos = quotes[QUO_TP_HC];
	else if((mastSit.obj[OBJT_N_COVER] != 0) || (mastSit.obj[OBJT_N_FFDS_COV] != 0))
		*pos = quotes[QUO_TP_HP];
	else
		*pos = quotes[QUO_TP];
				
	return(RT_NORMAL);
}